/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: Sounds.java 564 2010-07-25 20:54:57Z Harry $
 *   $LastChangedDate: 2010-07-25 20:54:57 +0000 (Sun, 25 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.resource;

import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 *
 * @author Harry Nguyen
 */
public enum Sounds {

    ERROR("/battlefield/sounds/error.wav"),
    MOUSE_HOVER("/battlefield/sounds/hover.wav"),
    MOUSE_CLICK("/battlefield/sounds/click.wav"),
    BOMB_HIT("/battlefield/sounds/revenge.wav"),
    BOMB_MISS("/battlefield/sounds/nooo.wav"),
    UNIT_SOLD("/battlefield/sounds/sold.wav"),
    GAME_STARTED("/battlefield/sounds/gamestarted.wav"),
    GAME_WON("/battlefield/sounds/youlose.wav"),
    GAME_LOSE("/battlefield/sounds/youwon.wav");
    private URL soundURL;
    private Clip clip;

    Sounds(String soundFileName) {
        AudioInputStream audioInputStream = null;
        soundURL = this.getClass().getResource(soundFileName);
        try {
            audioInputStream = AudioSystem.getAudioInputStream(soundURL);
            clip = AudioSystem.getClip();
            clip.open(audioInputStream);
        } catch (LineUnavailableException ex) {
            System.out.println(ex.toString());
        } catch (UnsupportedAudioFileException ex) {
            System.out.println(ex.toString());
        } catch (IOException ex) {
            System.out.println(ex.toString());
        } finally {
            try {
                if (audioInputStream != null) {
                    audioInputStream.close();
                }
            } catch (IOException ex) {
            }
        }
    }

    public void play() {
        clip.setFramePosition(0);
        clip.start();
    }
}
